<?php
/**
 * Created by PhpStorm.
 * User: a4041
 * Date: 2018/6/13
 * Time: 13:44
 */

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\ShopDish;
use App\Models\ShopDishAttribute;
use App\Models\ShopMenu;
use App\Repositories\ShopClerkRepository;
use App\Tools\Map;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\Shop;
use App\Models\Order;
use App\Models\ShopInfo;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
Class ShopMenuController extends Controller
{
    public function showAllMenu(Request $request){
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'shop_id.*' =>'商家不存在',
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        $res = ShopMenu::where('shop_id',$request->shop_id)->get();

        return show(200,'菜单分类信息',$res);
    }

    // 上传菜单分类图片
    public function upload(Request $request){
        if($_FILES['thumb']['size'] == 0 || $_FILES['thumb']['size']>1024*1024) {
            return show(201, '请上传1M以内的图片');
        }
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
            'menu_logo'=>'required|image'
        ];
        $message = [
            'shop_id.*' =>'商家不存在',
            'menu_logo.*' => '请上传图片'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $type = $request->file('thumb')->getClientOriginalExtension();
        if (!in_array($type,['jpg','jpeg','png'])){
            return show(201,'请上传png|jpg|jpeg格式的图片');
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        $path = $request->file('menu_logo')->storePublicly('shop_'.$request->shop_id.'/menu');
        return show(200,'上传成功','/storage/'. $path);
    }


    // 添加菜单分类
    public function addMenu(Request $request){
        $rules = [
            'shop_id'=>'required|integer|exists:shops,id',
            'menu_name' => 'required|string',
            'menu_sort' => 'integer',
            'menu_logo'=>'string',
        ];
        $message = [
            'menu_logo.*' => '菜单logo不正确',
            'menu_sort.*' => '排序不正确',
            'menu_name.*'=>'菜单分类名称有误',
            'shop_id.*' =>'商家不存在'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        try{
            $res = ShopMenu::create(\request(['menu_logo','menu_sort','menu_name','shop_id']));

        }catch (\Exception $e){
            dd($e->getMessage());
            return show(202,'添加菜单分类失败');
        }
        return show(200,'添加菜单分类成功');
    }

    // 根据ID获取菜单分类
    public function showOneMenu(Request $request){
        $rules = [
            'id'=>'required|integer|exists:shop_menu,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'shop_id.*' =>'商家不存在',
            'id.*' => '分类有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        $res = ShopMenu::find($request->id);

        return show(200,'菜单分类信息',$res);
    }

    // 获取某个分类下的所有菜式
    public function showOneMenuDish(Request $request){
        $rules = [
            'id'=>'required|integer|exists:shop_menu,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'shop_id.*' =>'商家不存在',
            'id.*' => '分类有误'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $res = ShopMenu::find($request->id);

        $dish = ShopDish::where('shop_id',$request->shop_id)->where('menu_id',$request->id)->get();
        if($dish){
            $dish = $dish->toArray();
            for ($i=0;$i<count($dish);$i++){
                if ($dish[$i]['is_attr'] == 1){
                    $dish[$i]['attribute'] = ShopDishAttribute::where('dish_id',$dish[$i]['id'])->get();
                    //处理多规格
                    for($y=0;$y<count($dish[$i]['attribute']);$y++)
                    {
                        $spec=explode(",", $dish[$i]['attribute'][$y]['spec']);
                        foreach($spec as $k2=>$v2)
                        {
                            $spec[$k2] = explode("|",$v2);
                            $specend[$k2]['spec_name'] = $spec[$k2][0];
                            $specend[$k2]['spec_price'] = $spec[$k2][1];
                        }
                        $dish[$i]['attribute'][$y]['spec']=$specend;
                    }
                }
            }

        }else{
            return show(202,'暂无该菜式');
        }


        return show(200,'菜单分类信息',$dish);
    }
    // 添加菜单分类
    public function editMenu(Request $request){
        $rules = [
            'id'=>'required|integer|exists:shop_menu,id',
            'menu_name' => 'required|string',
            'menu_sort' => 'integer',
            'menu_logo'=>'string',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'id.*'=>'分类ID有误',
            'menu_logo.*' => '菜单logo不正确',
            'menu_sort.*' => '排序不正确',
            'menu_name.*'=>'菜单分类名称有误',
            'shop_id.*' =>'商家不存在'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
        $user_id = Auth::id();
        if (!Shop::checkIsShop($request->shop_id,$user_id)){
            return show(203,'用户暂无该商家');
        }
        try{
            $res = ShopMenu::where('id',$request->id)->update(\request(['menu_logo','menu_sort','menu_name','shop_id']));

        }catch (\Exception $e){
            dd($e->getMessage());
            return show(202,'修改菜单分类失败');
        }
        return show(200,'修改菜单分类成功');
    }
    // 删除分类
    public function delMenu(Request $request){
        $rules = [
            'id'=>'required|integer',//|exists:shop_menu,id',
            'shop_id'=>'required|integer|exists:shops,id',
        ];
        $message = [
            'id.*'=>'分类ID有误',
            'shop_id.*' =>'商家不存在'
        ];
        $validator = Validator::make($request->all(), $rules,$message);
        if ($validator->fails()) {
            return show(201,$validator->errors()->first());
        }
//        $user_id = Auth::id();
//        if (!Shop::checkIsShop($request->shop_id,$user_id)){
//            return show(203,'用户暂无该商家');
//        }

        DB::beginTransaction();
        try{
            $res =ShopMenu::where('shop_id',$request->shop_id)->where('id',$request->id)->delete();
//            $res = DB::table('shop_menu')->where('shop_id',$request->shop_id)->delete($request->id);
           if ($res){
               $dish_ids =  DB::table('shop_dishes')->where('menu_id',$request->id)->get(['id']);
               if ($dish_ids->isEmpty()){
                    DB::commit();
               }
               foreach ($dish_ids as $ds){
                   $dish_id_attrs = DB::table('shop_dish_attribute')->where('dish_id',$ds->id)->get(['id']);
                   if ($dish_ids->isNotEmpty()){
                       foreach ($dish_id_attrs as $dr){
//                          DB::table('shop_dish_attribute')->delete($dr->id);
                          ShopDishAttribute::where('id',$dr->id)->delete();
                       }
                   }
//                   DB::table('shop_dishes')->where('menu_id',$request->id)->delete($ds->id);
                   ShopDish::where('menu_id',$request->id)->where('id',$ds->id)->delete();
               }

               DB::commit();
           }

        }catch (\Exception $e){
//            dd($e->getMessage());
            DB::rollBack();
            return show(202,'删除菜单分类失败');
        }
        return show(200,'删除菜单分类成功');
    }

}